package api

import (
	"net/http"
	"strconv"

	"GinAdmin/core/logger"
	"GinAdmin/model"
	"GinAdmin/service"

	"github.com/gin-gonic/gin"
)

// DepartmentAPI 部门API控制器
type DepartmentAPI struct {
	departmentService *service.DepartmentService
}

// NewDepartmentAPI 创建部门API控制器
func NewDepartmentAPI() *DepartmentAPI {
	return &DepartmentAPI{
		departmentService: service.NewDepartmentService(),
	}
}

// GetDepartmentList 获取部门列表
// @Summary 获取部门列表
// @Description 获取部门列表
// @Tags 部门管理
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param name query string false "部门名称"
// @Param status query int false "状态(1:启用,2:禁用)"
// @Success 200 {object} model.Response "成功"
// @Router /api/departments [get]
func (a *DepartmentAPI) GetDepartmentList(c *gin.Context) {
	params := &model.DepartmentSearchParams{
		Name:   c.Query("name"),
		Status: 0,
	}

	if status, err := strconv.Atoi(c.DefaultQuery("status", "0")); err == nil {
		params.Status = status
	}

	departments, err := a.departmentService.GetDepartmentList(params)
	if err != nil {
		logger.Error("获取部门列表失败: %v", err)
		c.JSON(http.StatusInternalServerError, model.Response{
			Code: 500,
			Msg:  "获取部门列表失败: " + err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, model.Response{
		Code: 200,
		Msg:  "获取成功",
		Data: departments,
	})
}

// GetDepartmentTree 获取部门树结构
// @Summary 获取部门树结构
// @Description 获取树形结构的部门列表
// @Tags 部门管理
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Success 200 {object} model.Response "成功"
// @Router /api/departments/tree [get]
func (a *DepartmentAPI) GetDepartmentTree(c *gin.Context) {
	tree, err := a.departmentService.GetDepartmentTree()
	if err != nil {
		logger.Error("获取部门树失败: %v", err)
		c.JSON(http.StatusInternalServerError, model.Response{
			Code: 500,
			Msg:  "获取部门树失败: " + err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, model.Response{
		Code: 200,
		Msg:  "获取成功",
		Data: tree,
	})
}

// GetDepartmentByID 获取部门详情
// @Summary 获取部门详情
// @Description 获取部门详情
// @Tags 部门管理
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param id path int true "部门ID"
// @Success 200 {object} model.Response "成功"
// @Router /api/departments/{id} [get]
func (a *DepartmentAPI) GetDepartmentByID(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 32)
	if err != nil {
		c.JSON(http.StatusBadRequest, model.Response{
			Code: 400,
			Msg:  "无效的部门ID",
		})
		return
	}

	department, err := a.departmentService.GetDepartmentByID(uint(id))
	if err != nil {
		logger.Error("获取部门详情失败: %v", err)
		c.JSON(http.StatusInternalServerError, model.Response{
			Code: 500,
			Msg:  "获取部门详情失败: " + err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, model.Response{
		Code: 200,
		Msg:  "获取成功",
		Data: department,
	})
}

// CreateDepartment 创建部门
// @Summary 创建部门
// @Description 创建新部门
// @Tags 部门管理
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param data body model.CreateDepartmentRequest true "部门信息"
// @Success 200 {object} model.Response "成功"
// @Router /api/departments [post]
func (a *DepartmentAPI) CreateDepartment(c *gin.Context) {
	var req model.CreateDepartmentRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, model.Response{
			Code: 400,
			Msg:  "请求参数错误: " + err.Error(),
		})
		return
	}

	department, err := a.departmentService.CreateDepartment(&req)
	if err != nil {
		logger.Error("创建部门失败: %v", err)
		c.JSON(http.StatusInternalServerError, model.Response{
			Code: 500,
			Msg:  "创建部门失败: " + err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, model.Response{
		Code: 200,
		Msg:  "创建成功",
		Data: department,
	})
}

// UpdateDepartment 更新部门
// @Summary 更新部门
// @Description 更新部门信息
// @Tags 部门管理
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param id path int true "部门ID"
// @Param data body model.UpdateDepartmentRequest true "部门信息"
// @Success 200 {object} model.Response "成功"
// @Router /api/departments/{id} [put]
func (a *DepartmentAPI) UpdateDepartment(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 32)
	if err != nil {
		c.JSON(http.StatusBadRequest, model.Response{
			Code: 400,
			Msg:  "无效的部门ID",
		})
		return
	}

	var req model.UpdateDepartmentRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, model.Response{
			Code: 400,
			Msg:  "请求参数错误: " + err.Error(),
		})
		return
	}

	department, err := a.departmentService.UpdateDepartment(uint(id), &req)
	if err != nil {
		logger.Error("更新部门失败: %v", err)
		c.JSON(http.StatusInternalServerError, model.Response{
			Code: 500,
			Msg:  "更新部门失败: " + err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, model.Response{
		Code: 200,
		Msg:  "更新成功",
		Data: department,
	})
}

// DeleteDepartment 删除部门
// @Summary 删除部门
// @Description 删除部门
// @Tags 部门管理
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param id path int true "部门ID"
// @Success 200 {object} model.Response "成功"
// @Router /api/departments/{id} [delete]
func (a *DepartmentAPI) DeleteDepartment(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 32)
	if err != nil {
		c.JSON(http.StatusBadRequest, model.Response{
			Code: 400,
			Msg:  "无效的部门ID",
		})
		return
	}

	err = a.departmentService.DeleteDepartment(uint(id))
	if err != nil {
		logger.Error("删除部门失败: %v", err)
		c.JSON(http.StatusInternalServerError, model.Response{
			Code: 500,
			Msg:  "删除部门失败: " + err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, model.Response{
		Code: 200,
		Msg:  "删除成功",
	})
}
